<?php
/**
 +------------------------------------------------------------------------------
 * 常量模型
 +------------------------------------------------------------------------------
 */
class ConstantModel extends CommonModel
{
	protected $_validate = array(
		array('constant_name','require','{%NAME_REQUIRE}'),
                array('constant_name','','{%NAME_UNIQUE}',0,'unique',2),
                array('constant_title','require','{%TITLE_REQUIRE}'),
                array('sort','number','{%SORT_REQUIRE}'),
	);
        protected $_auto=array(
            array('operatetime','time',3,'function'),
        );

    /**
     * 获取适合下拉列表使用的节点数据
     * 
     * @param int $constant_id 常量ID
     * @param int $selected_id 默认选中节点ID
     */
    public function get_node_option($constant_id, $selected_id=0) {
        $list = M('ConstantNode')->where("constant_id={$constant_id}")->order('sort')->select();
        load('extend');
        $tree = list_to_tree($list, 'id', 'parentid', '_child');
        $this->digui_option($tree, 0, $selected_id);
        return $this->digui_str;
    }
    
    
    
    var $digui_str = '';
    /**
     *递归输出option格式的数据字符串
     * 
     * @param array $tree 无限级分类树数组
     * @param int $tab 递归的等级 默认0调用即可
     * @param int $id 默认选中的数据 
     * 
     * @return String 用于select显示的option字符串
     */
    public function digui_option($tree, $tab = 0, $id = 0) {
        foreach ($tree as $key => $val) {
            $this->digui_str.='<option value="' . $val['id'] . '"';
            if ($val['id'] == $id) {
                $this->digui_str.=' selected="selected"';
            }
            $this->digui_str.='>';
            $this->digui_str.=str_repeat('┝', $tab);
            $this->digui_str.=$val['name'];
            $this->digui_str.='</option>';
            if (isset($val['_child'])) {
                $this->digui_option($val['_child'], $tab + 1, $id);
            }
        }
    }
    
    
    /**
     * 获取文本输出的无限级递归展示
     * 
     * @param int $constant_id 常量ID
     * @param int $selected_id 默认选中节点ID
     */
    public function get_all_node($constant_id) {
        $list = M('ConstantNode')->where("constant_id={$constant_id}")->order('sort')->select();
        load('extend');
        $tree = list_to_tree($list, 'id', 'parentid', '_child');
        $this->digui_text($tree, 0);
        return $this->digui_str;
    }
    
    
    /**
     *用于文本显示的无限级分类递归输出
     * 
     * @param array $tree 无限级分类树数组
     * @param int $tab 递归的等级 默认0调用即可
     * 
     * @return String 文本字符串
     */
    public function digui_text($tree, $tab = 0) {
        foreach ($tree as $key => $val) {
            $this->digui_str.=str_repeat('┝', $tab);
            $this->digui_str.=$tab==0?'<font color="blue"><b>':'';
            $this->digui_str.=$val['name'];
            $this->digui_str.=$tab==0?'</b></font>':'';
            $this->digui_str.='<br />';
            if (isset($val['_child'])) {
                $this->digui_text($val['_child'], $tab + 1);
            }
        }
    }
}
?>